home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Halp! I don't know what I'm doing wrong!
- Date: 4 Feb 1996 21:29:28 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4f38fo$6ri@news.iag.net>
- References: <4eu8sl$f27@aphex.direct.ca>
- NNTP-Posting-Host: pm3-orl21.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4eu8sl$f27@aphex.direct.ca>, etoivane@direct.ca says...
- >
- >I've been at this for a couple *days* and can't get it to work.
- >It's supposed to graph a function entered at the command line,
- >eg. "c:\>graph 2 1" should graph out 2x+1 to stdout using the '*'
- >character. Can anybody help me please?
- >I'll name all my kids after you!(when I have 'em that is!)
- >
- >/*
- > Ed Toivanen
- > Comp3425
- > Assign1
- >*/
- >
- >#include <stdio.h>
- >#include <stdlib.h>
- >
- >#define DOMAN 80
-
- Depending on your output device, this value might cause double-spacing.
-
- >#define DCORR 40/* shift right, to keep in domain of array*/
- >#define RANGE 40
- >#define RCORR 20/* shift up, to keep in range of array*/
- >
- >long power(long, int);
- >
- >int main(int argc, char **argv){
- > long minDomain = -DOMAN/2;
- > long maxDomain = DOMAN/2-1;
- > long minRange = -RANGE/2;
- > long maxRange = RANGE/2-1;
- > static char lin[RANGE][DOMAN]; /*
- > long x, y=0;
- > long degree, argctemp=argc;
- >
- > if(argc < 2){
- > printf("Usage %s <coefficients>\n", *argv);
- > return(0);
- > }
- >
- > for(x=minDomain; x<maxDomain; x++){
- > degree = argc - 1;
-
- You need to reset y to 0 on each iteration of the for loop.
-
- > while(degree--){
- > y = ((atol(*++argv)) * (power(x, degree))) + y;
-
- Because you are actually incrementing argv, after the first iteration of
- the for loop, you will no longer be looking at valid pointers. Try something
- like:
-
- atol(argv[argc - degree - 1])
-
- You are getting a skewwed results. (eg, the point 0,0 with the args 0 1) You
- might want to drop out of the while loop before the final argument and simply
- add it to y instead of running through power.
-
- My compiler warns about degree being long, when power accepts an int. It is
- not relevant in the range of values present (degree will not exceed INT_MAX),
- but you might want to match the types anyway.
-
- > if(y > maxRange || y < minRange)
- > y = 0;
-
- ?? Won't this cause a point not in the line to be plotted?
-
- > }
-
- You need to add a condition to protect your array bounds here. Something like
-
- if (y >= minRange && y <= maxRange)
-
- > lin[ y + RCORR][x + DCORR] = '*';
- > }
- >
- > for(y=maxRange; y>minRange; y--){/*from top down*/
- > for(x = minDomain; x < maxDomain; x++){
- > printf("%c", lin[y + RCORR][x + DCORR]);
-
- Because lin is defined as static, all elements are initialized to '\0'. So
- this is sending '\0's to stdout. I am not sure what the result will be on
- your system, but I suspect that it will not be what you want.
-
- > }
- > printf("\n");
- > }
-
- If you will define lin with one extra char in each line to allow for a '\0',
- then prior to your plotting loop, use memset in a for loop to initialize all
- chars in each line to ' ' (except for the terminating '\0'), you will be able
- to replace this nested loop with a single loop that steps through the RANGE
- and prints each line.
-
- > return(0);
- >}
- >
- >long power(long num, int degree){/* for positive,
- >whole powers*/
- > long product = 1L;
- >
-
- You should test for illegal degree values and handle the possibility
-
- > if(degree==0 && num==0)
-
- The degree condition is not necessary. 0 to any power is 0.
-
- > return(0L);
- > if(degree==0)
- > return(1L);
-
- The while condition and initialization of product make this test unnecessary
-
- > while(degree--){
- > product *= num;
- > }
- > return(product);
- >}
- >
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-